package orm.base;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.reflections.Reflections;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import orm.base.Ex.BaseEx;
import orm.config.ProviderName;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


/**
 * 服务基类
 */
public abstract class BaseService extends BaseEx {
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;
	private TransactionFactory transactionFactory = new JdbcTransactionFactory();

	protected static Map<String, Class<?>> providers = new HashMap<>();


	//缓存全部的字典信息
	static {
		Set<Class<?>> classSet = new Reflections("orm.dictionary").getTypesAnnotatedWith(ProviderName.class);
		for (Class c : classSet) {
			providers.put(((ProviderName) c.getAnnotation(ProviderName.class)).name(), c);
		}
	}

	public JSONObject getEnumType(Map map) throws Exception{
		if(map.get("type") == null || "".equals(map.get("type").toString()))
			return this.codeMess(eCode,"枚举类型实例不能为空");
		Class<?> clazz = providers.get(map.get("type").toString());
		if(clazz == null)
			return this.codeMess(eCode,"枚举类型有误");
		Method m = clazz.getDeclaredMethod("values");
		Enum[] enumArr = (Enum[])m.invoke(Enum.class);
		JSONArray jsonArray = new JSONArray();
		for(int i = 0 ; i < enumArr.length;i++){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("val",String.valueOf(enumArr[i]));
			jsonObject.put("text",enumArr[i]);
			jsonArray.add(jsonObject);
		}
		return this.codeData(jsonArray);
	}

	public JSONObject getEnumTypeSingle(Map map) throws Exception{
		if(map.get("type") == null || "".equals(map.get("type").toString()))
			return this.codeMess(eCode,"枚举类型实例不能为空");
		Class<?> clazz = providers.get(map.get("type").toString());
		if(clazz == null)
			return this.codeMess(eCode,"枚举类型有误");
		Method m = clazz.getDeclaredMethod("values");
		Enum[] enumArr = (Enum[])m.invoke(Enum.class);
		JSONObject jsonObject = new JSONObject();
		for(int i = 0 ; i < enumArr.length;i++){
			jsonObject.put(String.valueOf(enumArr[i]),enumArr[i]);
		}
		return this.codeData(jsonObject);
	}



	/**
	 * 获得一个事物
	 *
	 * @return
	 */
	public Transaction getTramsaction() {
		return transactionFactory.newTransaction(sqlSessionTemplate.getConnection());
	}

	/**
	 * 手动回滚事务
	 */
	public void rollBack() {
		TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	}

	/**
	 * 字典-->具体名称
	 *
	 * @return
	 */
	public String getOrdinalHandlerVal(Object object, String property, String value) {
		return null;
	}

	/**
	 * 获得当前方法名
	 *
	 * @return
	 */
	public String getMethodName() {
		return Thread.currentThread().getStackTrace()[1].getMethodName();
	}

}
